﻿//-----------------------------------------------------------------------
// <copyright file="ExceptionEntity.cs" company="CodePlex">
//     Copyright (c) Doug Holland, CodePlex. All rights reserved.
// </copyright>
//-----------------------------------------------------------------------
namespace CodePlex.Diagnostics.Model
{
    using System;

    using CodePlex.Diagnostics.Contracts;

    /// <summary>
    /// Represents errors that occur during application execution.
    /// </summary>
    public sealed partial class ExceptionEntity
    {
        #region public ExceptionEntity()

        /// <summary>
        /// Initializes a new instance of the <see cref="T:ExceptionEntity"/> class.
        /// </summary>
        public ExceptionEntity()
        {
        }
        
        #endregion

        #region public ExceptionEntity(ExceptionDataContract ex, IIdentityDataContract identity, Guid publicationId)

        /// <summary>
        /// Initializes a new instance of the <see cref="T:ExceptionEntity"/> class.
        /// </summary>
        /// <param name="ex">An instance of the <see cref="T:ExceptionDataContract"/> class to be used to construct an instance of the <see cref="T:ExceptionEntity"/> class.</param>
        /// <param name="identity">An instance of the <see cref="T:IIdentityDataContract"/> class to be used to construct an instance of the <see cref="T:ExceptionEntity"/> class.</param>
        /// <param name="PublicationId">The publication Id associated with the <see cref="T:ExceptionEntity"/>.</param>
        public ExceptionEntity(ExceptionDataContract ex, IIdentityDataContract identity, Guid publicationId)
        {
            if (ex.AppDomain != null)
            {
                this.AppDomainEntity = new AppDomainEntity(ex.AppDomain);
            }

            if (ex.Assembly != null)
            {
                this.AssemblyEntity = new AssemblyEntity(ex.Assembly);
            }

            this.ExceptionType = ex.ExceptionType;

            this.HelpLink = ex.HelpLink;

            this.HResult = ex.HResult;

            this.IIdentityAuthenticationType = identity.AuthenticationType;

            this.IIdentityIsAuthenticated = identity.IsAuthenticated;

            this.IIdentityName = identity.Name;

            this.IsTransient = ex.IsTransient;

            if (ex.Machine != null)
            {
                this.MachineEntity = new MachineEntity(ex.Machine);
            }

            this.Message = ex.Message;

            if (ex.Process != null)
            {
                this.ProcessEntity = new ProcessEntity(ex.Process);
            }

            this.PublicationId = publicationId;

            this.SerializedExceptionXml = ex.SerializedException;

            this.SerializedIIdentityXml = identity.SerializedIIdentity;

            this.Source = ex.Source;

            this.StackTrace = ex.StackTrace;

            this.Unhandled = ex.Unhandled;

            if (ex.TargetSite != null)
            {
                this.TargetSiteEntity = new TargetSiteEntity(ex.TargetSite);
            }

            if (ex.Thread != null)
            {
                this.ThreadEntity = new ThreadEntity(ex.Thread);
            }

            if (ex.InnerExceptions != null)
            {
                foreach (var contract in ex.InnerExceptions)
                {
                    ExceptionEntity innerExceptionEntity = new ExceptionEntity(contract, identity, this.PublicationId);

                    innerExceptionEntity.AssemblyEntity = this.AssemblyEntity;

                    innerExceptionEntity.AppDomainEntity = this.AppDomainEntity;

                    innerExceptionEntity.MachineEntity = this.MachineEntity;

                    innerExceptionEntity.ProcessEntity = this.ProcessEntity;

                    innerExceptionEntity.ThreadEntity = this.ThreadEntity;

                    this.InnerExceptionEntities.Add(innerExceptionEntity);
                }
            }
        }

        #endregion
    }
}
